//go:build windows

package idPool8Bit

import (
	"os"
	"syscall"
	"unsafe"
)

func mmapFile(fileName string, size int) ([]byte, error) {
	file, err := os.OpenFile(fileName, os.O_RDWR, 0644)
	if err != nil {
		return nil, err
	}

	// 获取文件句柄
	h := syscall.Handle(file.Fd())

	// 创建文件映射
	hMap, err := syscall.CreateFileMapping(h, nil, syscall.PAGE_READWRITE, 0, uint32(size), nil)
	if err != nil {
		return nil, err
	}

	// 将文件映射到内存
	addr, err := syscall.MapViewOfFile(hMap, syscall.FILE_MAP_WRITE, 0, 0, uintptr(size))
	if err != nil {
		return nil, err
	}

	// 转换为 byte 切片
	data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), size)
	return data, nil
}

func munmapFile(data []byte) error {
	addr := uintptr(unsafe.Pointer(&data[0]))
	return syscall.UnmapViewOfFile(addr)
}
